package per.com.service.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import per.com.bean.User;
import per.com.common.Const;
import per.com.common.ServerResponse;
import per.com.dao.PcUsersDao;
import per.com.service.IUserManageService;

import java.util.List;

/**
 * @author zxy
 */
@Service("iUserManageService")
public class UserManageServiceImpl implements IUserManageService {

    @Autowired
    @Qualifier("PcUsersDaoId")
    private PcUsersDao pcUsersDao;

    @Override
    public ServerResponse<PageInfo> userList(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum , pageSize);
        List<User> userList = pcUsersDao.selectListUser();
        PageInfo pageInfo =  new PageInfo(userList);

        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse freeze(String userId , Integer isfreeze) {
        if (StringUtils.isBlank(userId)){
            return ServerResponse.createByErrorMessage("请传入正确参数");
        }

        User user = new User();
        user.setUid(userId);
        user.setIsfreeze(isfreeze);
        int resultCount = pcUsersDao.updateByPrimaryKeySelective(user);
        if (resultCount > 0){
            return ServerResponse.createBySuccessMessage("修改成功");
        }
        return ServerResponse.createByErrorMessage("修改失败");
    }

    @Override
    public ServerResponse updateCreditScore(String userId, Integer creditScore) {
        if (StringUtils.isBlank(userId)){
            return ServerResponse.createByErrorMessage("请传入正确参数");
        }
        User user = new User();
        user.setUid(userId);
        user.setCreditScore(creditScore);
        int resultCount = pcUsersDao.updateByPrimaryKeySelective(user);
        if (resultCount > 0){
            return ServerResponse.createBySuccessMessage("修改成功");
        }
        return ServerResponse.createByErrorMessage("修改失败");
    }

    @Override
    public ServerResponse checkAdminRole(User user) {
        if (user != null && user.getStatus().intValue() == Const.Role.ROLE_ADMIN){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }


}
